package com.banksoft.admin.controller;

import com.banksoft.admin.bean.AmountType;
import com.banksoft.admin.bean.MemberType;
import com.banksoft.admin.dao.AmountLogDao;
import com.banksoft.admin.dao.UserDao;
import com.banksoft.admin.dao.UserMoneyDao;
import com.banksoft.admin.entity.User;
import com.banksoft.admin.entity.UserMoney;
import com.banksoft.admin.service.BaseService;
import com.banksoft.admin.service.UserMoneyService;
import com.banksoft.common.BaseController;
import com.banksoft.common.PageInfo;
import com.banksoft.util.DateUtil;
import com.banksoft.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by vanjoger on 2017/1/5.
 */
@Controller
@RequestMapping("/userAmount")
public class UserAmountController extends BaseController{

    @Autowired
    BaseService baseService;
    @Autowired
    UserMoneyService userMoneyService;


    @RequestMapping("/list")
    public String list(String type,Model model){
        model.addAttribute("type",type);
        return "user/user_amount_list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(String nickName,String type,String account,String status,PageInfo pageInfo){
        Map<String,Object> map = new HashMap<>();
        String[] types = new String[]{type};
        map.put("account",account);
        map.put("type",types);
        map.put("nickName",nickName);
        map.put("isMember","1");

        pageInfo.setParams(map);

        pageInfo = this.page(UserMoneyDao.class,pageInfo);

        return this.getPageResult(pageInfo);
    }

    @RequestMapping("/listRecord")
    public String listRecord(Model model){
        return "user/hand_tz_list";
    }

    @RequestMapping("/listRecordData")
    @ResponseBody
    public Object listRecordData(String nickName,String account,String startDate,String endDate,PageInfo pageInfo){
        Map<String,Object> map = new HashMap<>();
        map.put("nickName",nickName);
        map.put("account",account);
        map.put("type", new String[]{AmountType.HandleFee.name()});
        if(StringUtil.isNotNull(startDate))
            map.put("startDate", DateUtil.getLongFormStrDate(startDate));
        if(StringUtil.isNotNull(endDate))
            map.put("endDate",DateUtil.getLongFormStrDate(endDate));

        pageInfo.setParams(map);

        pageInfo = this.page(AmountLogDao.class,pageInfo);

        return this.getPageResult(pageInfo);
    }

    @RequestMapping("/save")
    @ResponseBody
    public JSONObject save(UserMoney user){
        try{
            this.baseService.update(UserMoneyDao.class,user);
            return this.FmtResult(true,"保存成功",null);
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.FmtResult(false,"保存失败",null);
    }


    @RequestMapping("/viewdetail")
    public String viewdetail(String id,Model model){
        User user = this.baseService.findById(UserDao.class,id);
        UserMoney money = this.userMoneyService.findByUserId(id);
        model.addAttribute("data",user);
        model.addAttribute("money",money);
        return "user/user_amount_list_detail";
    }

    @RequestMapping("/listDetails")
    @ResponseBody
    public Object listDetails(String userId,String startDate,String endDate,PageInfo pageInfo){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        if(StringUtil.isNotNull(startDate))
            map.put("startDate", DateUtil.getLongFormStrDate(startDate));
        if(StringUtil.isNotNull(endDate))
            map.put("endDate",DateUtil.getLongFormStrDate(endDate));

        pageInfo.setParams(map);

        pageInfo = this.page(AmountLogDao.class,pageInfo);

        return this.getPageResult(pageInfo);
    }


    @RequestMapping("/viewAmount")
    public String viewAmount(String id,Model model){
        UserMoney money = this.baseService.findById(UserMoneyDao.class,id);
        model.addAttribute("money",money);
        if(StringUtil.isNotNull(money.getUserId())){
            User user = this.baseService.findById(UserDao.class,money.getUserId());
            model.addAttribute("data",user);
        }
        return "user/user_amount_view";
    }

    @RequestMapping("/updateAmount")
    @ResponseBody
    public JSONObject updateAmount(String id,String type,String amount,HttpServletRequest request){
        try{
            JSONObject json = this.userMoneyService.updateAmount(id,type,amount,request);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,"调整余额失败",null);
        }
    }
}
